#include<stdio.h>
#include<stdlib.h>
#include<unistd.h>
#include<string.h>
#include<sys/types.h>
#include<sys/wait.h>
int show()
{
  printf("hello show()!\n");
  sleep(1);
  _exit(12);
  //return 0;
}

int main()
{
  pid_t ids[10];
  for(int i=0;i<10;i++)
  {
    pid_t id =fork();
    if(id == 0){
      //child
      int count = 10;
      while(count > 0)
      {
        printf("child do something!: %d,%d\n",getpid(),getppid());
        sleep(1);
        count--;
      }
      exit(i);
    }
    //father
    ids[i] = id;
  }

  int count  = 0;
  while(count<10){
    int status = 0;
    pid_t ret = waitpid(ids[count],&status,0);
    if(ret>=0){
      printf("wait child success!, %d\n",ret);
      if(WIFEXITED(status)){
        printf("child exit code: %d\n",WEXITSTATUS(status));
      }
      else{
        printf("child not exit normal!\n");
      }
    //  printf("status: %d\n",status);
    //  printf("child exit code: %d\n",(status>>8) & 0xFF);
    //  printf("child get signal: %d\n",status & 0x7F);
    }
    count++;
  }

  //if(id == 0){
  //  //child
  //  int count = 0;
  //  while(count < 30)
  //  {
  //    printf("I am child,pid: %d,ppid: %d\n",getpid(),getppid());
  //    count++;
  //    if(count == 10){
  //      //int a = 1/0;
  //      int *p;
  //      *p = 100;
  //    }
  //    sleep(1);
  //  }
  //  exit(11);
  //}
  //else{
  //  //father 
  //    printf("I am father,pid: %d,ppid: %d\n",getpid(),getppid());
  //    //pid_t ret = wait(NULL);
  //    int status = 0;
  //    pid_t ret = waitpid(id,&status,0);
  //    if(ret>=0){
  //      printf("wait child success!, %d\n",ret);
  //      printf("status: %d\n",status);
  //      printf("child exit code: %d\n",(status>>8) & 0xFF);
  //      printf("child get signal: %d\n",status & 0x7F);
  //    }

  //    printf("Father run ....\n");
  //    sleep(10);
  //}
  //int* p;
  //*p =100;

 // while(1)
 // {
 //   printf(".\n");
 // }
  //printf("hello world!");
  //sleep(3);
  //exit(0);
  //printf("\n...................................\n");
  //sleep(3);
  //for(int i = 0;i < 150; i++)
  //{
  //  printf("%d: %s\n",i,strerror(i));
  //}
  //show();
  //printf("process is not done!\n");
  //return 0;
}
